package com.yld.car.market;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import cn.yld.car.db.RecentlyViewedDBHelper;

import com.yld.car.common.ConstantUtils;
import com.yld.car.common.NetworkProgressUtils;
import com.yld.car.domain.Car;

public class CarSeriesActivity extends BaseActivity implements
		OnItemClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		// 自定义标题栏
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.series_base_list);
		// 设置布局
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		((TextView) findViewById(R.id.title)).setText("选择车系");
		super.onCreate(savedInstanceState);

		this.showBackButton();
		listView = (ListView) findViewById(R.id.list1);

		listView.setOnItemClickListener(this);
		mBar = (ProgressBar) findViewById(R.id.progressBar);
		utils = NetworkProgressUtils.getInstance();
		Intent intent = getIntent();

		String cId = intent.getStringExtra("cId");
		flag = intent.getBooleanExtra("flag", false);
		// System.out.println("cId===" + cId);
		if (cId != null) {
			new SeriesCarTask().execute(cId);
		} else {
			Toast.makeText(this, "获取数据异常！", Toast.LENGTH_SHORT).show();
		}

	}

	private ProgressBar mBar;
	private ListView listView = null;
	private ArrayList<Car> all = new ArrayList<Car>();

	private class SeriesCarAdapter extends BaseAdapter {

		private ArrayList<Car> all;

		public SeriesCarAdapter(ArrayList<Car> all) {
			this.all = all;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return all.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return all.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				convertView = LayoutInflater.from(getApplicationContext())
						.inflate(R.layout.my_item_car, null);
			}

			TextView t1 = (TextView) convertView.findViewById(R.id.itemTitle);
			t1.setTextColor(getResources().getColor(R.color.black_light));
			t1.setText(all.get(position).getName());
			// TextView t2 = (TextView)
			// convertView.findViewById(R.id.itemContent);
			// t2.setText(all.get(position).getName());
			return convertView;
		}

	}

	private NetworkProgressUtils utils = null;

	private class SeriesCarTask extends
			AsyncTask<String, String, ArrayList<Car>> {

		@Override
		protected ArrayList<Car> doInBackground(String... params) {

			if (utils.getAPNType(getApplicationContext()) == -1) {

				return null;
			} else {

				HashMap<String, String> allParams = new HashMap<String, String>();

				allParams.put("bId", params[0]);
				Object o = utils.getWebservicesByData(ConstantUtils.NAME_SPACE,
						ConstantUtils.ALL_METHROD_NAME.get(9),
						ConstantUtils.NAME_SPACE
								+ ConstantUtils.ALL_METHROD_NAME.get(9),
						ConstantUtils.GET_SERIES_BY_BRAND_ID_URL, allParams);

				if (o.equals("")) {

					return null;
				}
				all = utils.parseJson(o.toString(), "table",
						NetworkProgressUtils.BASE_CAR_TYPE,true);
				// System.out.println("CarSeries====>" + o);

				return all;
			}

		}

		@Override
		protected void onPreExecute() {

			mBar.setVisibility(View.VISIBLE);
			super.onPreExecute();
		}

		@Override
		protected void onPostExecute(ArrayList<Car> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			mBar.setVisibility(View.GONE);
			if (result == null) {

				Toast.makeText(getApplicationContext(), "网络未连接，请设置网络!",
						Toast.LENGTH_SHORT).show();
			} else {
				SeriesCarAdapter adapter = new SeriesCarAdapter(result);
				listView.setAdapter(adapter);
			}

		}

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View v, int position,
			long id) {

		final Car car = (Car) adapterView.getAdapter().getItem(position);
		if (flag) {
			this.mApp.setmSeriesCar(car);
			new Thread(new Runnable() {

				@Override
				public void run() {

					/*
					 * <classId>string</classId> <color>string</color>
					 * <deploy>string</deploy> <port>string</port>
					 * <nowPage>string</nowPage> <pageSize>string</pageSize>
					 */
					HashMap<String, String> allParams = new HashMap<String, String>();
//					
					allParams.put("classId", car.getId());
					allParams.put("color", "");
					allParams.put("deploy", "");
					allParams.put("port", "");
					allParams.put("nowPage", "1");
					allParams.put("pageSize", "20");
					//allParams.put("keyWords", car.getId());
//					allParams.put("color", "");
//					allParams.put("type", "");
//					allParams.put("port", "");
//					allParams.put("flag", "1");
//					allParams.put("nowPage", "1");
//					allParams.put("pageSize", "20");
					Object o = utils.getWebservicesByData(
							ConstantUtils.NAME_SPACE,
							ConstantUtils.ALL_METHROD_NAME.get(38),
							ConstantUtils.NAME_SPACE
									+ ConstantUtils.ALL_METHROD_NAME.get(38),
							ConstantUtils.GET_PAGE_DATA_CARS_NEW_URL, allParams);

					//System.out.println("carS---" + o);
					// System.out.println("=======================");
					ArrayList<Car> all = utils.parseJson(o.toString(), "table",
							NetworkProgressUtils.SEARCH_CAR_TYPE,true);

					//System.out.println("T:------->" + all);
					mApp.setAllDetails(all);
				}
			}).start();
			finish();
			return;
		}
		// String name = car.getName();
		String carId = car.getId();
		this.mApp.setClassId(carId);
		// Intent intent = new Intent();
		// intent.setClass(this, CarTypeInfoActivity.class);
		//
		// intent.putExtra("name", name);
		// intent.putExtra("carId", carId);
		// startActivity(intent);
		finish();

	}

	private boolean flag = false;

}
